<?php
/**
 * FastApp.
 * 10/8/24
 * @Author deepsea159
 * @Contact telegram:deepsea159
 */

namespace App\Game\Job;

use App\Game\Api\Service\AccountService;
use Carbon\Carbon;
use Hyperf\AsyncQueue\Job;
use App\Fastapp\Api\Service\Account\WalletService;
use App\Fastapp\Model\Account;
use App\Fastapp\Model\AccountRecharge;
use App\Fastapp\Model\AccountWalletLog;

class RechargeJob extends Job
{

    public function __construct(public int $user_id, public int $data_id, public int $account_type)
    {
    }


    public function handle(): void
    {
        try {
            $md = AccountRecharge::query()->where(['id' => $this->data_id, 'recharge' => 0])->first();
            if (!$md) return;
            $md->update(['recharge' => 1]);
            AccountService::effectiveLevel($this->user_id, $this->account_type);
            $setting = api_cache()->getAppSetting($this->account_type);
            if (!empty($setting['register_balance_winnings'])) {
                if (AccountWalletLog::query()->where(['user_id' => $this->user_id, 'field' => 'balance_winnings'])->exists()) return;
                $ac = Account::query()->where(['gene_id' => $this->user_id])->first(['created_at']);
                if ($ac->created_at->addHours((int)$setting['register_balance_winnings_tm'])->gt(Carbon::now())) {
                    $data['amount'] = $setting['register_balance_winnings'];
                    WalletService::inc($data, field: 'balance_winnings');
                }
            }
        } catch (\Throwable $throwable) {
            console()->error($throwable->getMessage());
        }
    }
}